package com.bazinga.contact.view;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.bazinga.contact.controller.JobDetailsManager;
import com.bazinga.contact.controller.JobScheduleManager;
import com.bazinga.contact.model.AppliedUser;
import com.bazinga.contact.model.JobDetails;
import com.bazinga.contact.model.JobSchedule;
import com.opensymphony.xwork2.ActionSupport;


public class CreatedJobDetailsAction extends ActionSupport{

	private static final long serialVersionUID = 9149826260758390091L;
	private JobDetails jobDetails;
	private List<JobSchedule> jobScheduleList;
	private Long id;
	private String daysofweek="";
	private String startTime;
	private String endTime;
	private List<AppliedUser> appUserList;
	
	private JobDetailsManager linkController;
	private JobScheduleManager jsLinkController;
	
	public JobDetails getJobDetails() {
		return jobDetails;
	}

	public void setJobDetails(JobDetails jobDetails) {
		this.jobDetails = jobDetails;
	}

	public CreatedJobDetailsAction() {
		linkController = new JobDetailsManager();
		jsLinkController = new JobScheduleManager();
		appUserList = new ArrayList<AppliedUser>();
	}

	public String execute() {
		jobDetails = linkController.getById(id);
		jobScheduleList =  jsLinkController.getByJobId(id);
		for (JobSchedule jobSch:jobScheduleList){
			if("1".equals(jobSch.getDayofweek())){
				daysofweek += " Monday ";
			}else if("2".equals(jobSch.getDayofweek())){
				daysofweek += " Tuesday ";
			}else if("3".equals(jobSch.getDayofweek())){
				daysofweek += " Wednesday ";
			}else if("4".equals(jobSch.getDayofweek())){
				daysofweek += " Thrusday ";
			}else if("5".equals(jobSch.getDayofweek())){
				daysofweek += " Friday ";
			}else if("6".equals(jobSch.getDayofweek())){
				daysofweek += " Saturday ";
			}else if("7".equals(jobSch.getDayofweek())){
				daysofweek += " Sunday ";
			}
			startTime = jobSch.getStart_time().toString();
			endTime = jobSch.getEnd_time().toString();
		}
		getUserAppliedList();
		return SUCCESS;
	}	

	public String delete() {
		linkController.delete(getId());
		return SUCCESS;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public List<JobSchedule> getJobScheduleList() {
		return jobScheduleList;
	}

	public void setJobScheduleList(List<JobSchedule> jobScheduleList) {
		this.jobScheduleList = jobScheduleList;
	}

	public String getDaysofweek() {
		return daysofweek;
	}

	public void setDaysofweek(String daysofweek) {
		this.daysofweek = daysofweek;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}
	
	private void getUserAppliedList(){
		String dbUrl = "jdbc:mysql://127.0.0.1:3306/";
		String dbName = "bazinga";
		String driverName = "org.gjt.mm.mysql.Driver";
		String username = "root";
		String password = "";
		ResultSet rs = null;
		
		Connection conn = null;
		Statement stmt = null;
				
		String searchQuery = "select U.fname,U.lname,U.userid,A.status from userdetails U, applications A where A.userid = U.userid and A.jobid='" + id + "' ;";
		try
		{
			Class.forName(driverName).newInstance();
			conn = DriverManager.getConnection(dbUrl + dbName, username, password);
			stmt = conn.createStatement();
		}
		catch (Exception e) {
			System.out.println(e.toString());
		}
		try {
			rs = stmt.executeQuery(searchQuery);
			while (rs.next()) {
				AppliedUser aUser = new AppliedUser();
				aUser.setUserid(new Long(rs.getString("userid")));
				aUser.setFname(rs.getString("fname"));
				aUser.setLname(rs.getString("lname"));
				if(rs.getString("status").equals("0"))
					aUser.setStatus("Pending");
				else if(rs.getString("status").equals("1"))
					aUser.setStatus("In Progress");
				else
					aUser.setStatus("Completed");
				appUserList.add(aUser);
			}
		} 
		
		catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public List<AppliedUser> getAppUserList() {
		return appUserList;
	}

	public void setAppUserList(List<AppliedUser> appUserList) {
		this.appUserList = appUserList;
	}
	
	
}
